package com.yh.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.yh.bean.HelpFirst;
import com.yh.bean.HelpInfo;
import com.yh.bean.HelpSecond;
import com.yh.service.HelpService;

@Controller
public class HelpController {

	@Autowired
	private HelpService helpService;
	
	@RequestMapping("/first")
	@ResponseBody 
	public List<HelpFirst> loadHelpFirst(){
		System.out.println("=======进入一级分类功能=======");
		List<HelpFirst> firstList = helpService.lookHelpFirst();
		System.out.println("============"+firstList);
		return firstList;
	}
	
	@RequestMapping("/second")
	@ResponseBody
	public List<HelpSecond> loadHelpSecond(int firId){
		System.out.println("=======进入二级分类功能=======");
		List<HelpSecond> secondList = helpService.lookHelpSecond(firId);
		System.out.println("============"+secondList);
		return secondList;
	}
	
	@RequestMapping("/info")
	@ResponseBody
	public List<HelpInfo> loadHelpInfo(int secId){
		System.out.println("***********************************"+secId);
		System.out.println("=======进入帮助信息功能=======");
		List<HelpInfo> infoList = helpService.lookHelpInfo(secId);
		System.out.println("============"+infoList);
		return infoList;
	}
	
	@RequestMapping("/help")
	public String Help(int secId,HttpServletRequest request){
		System.out.println("页面判断");
		System.out.println("*****************"+secId);
		HttpSession session = request.getSession();
		session.setAttribute("secId", secId);
		String result="";

		if(secId==1){
			result="manager/help1";
		}else if(secId==2){
			result="manager/help2";
		}else if(secId==3){
			result="manager/help3";
		}else if(secId==4){
			result="manager/help4";
		}else if(secId==5){
			result="manager/help5";
		}else if(secId==6){
			result="manager/help6";
		}else if(secId==7){
			result="manager/help7";
		}else if(secId==8){
			result="manager/help8";
		}else if(secId==9){
			result="manager/help9";
		}else if(secId==10){
			result="manager/help10";
		}else if(secId==11){
			result="manager/help11";
		}else if(secId==12){
			result="manager/help12";
		}else if(secId==13){
			result="manager/help13";
		}else if(secId==14){
			result="manager/help14";
		}else if(secId==15){
			result="manager/help15";
		}else if(secId==16){
			result="manager/help16";
		}else if(secId==17){
			result="manager/help17";
		}else if(secId==18){
			result="manager/help18";
		}else if(secId==19){
			result="manager/help19";
		}else if(secId==20){
			result="manager/help20";
		}else if(secId==21){
			result="manager/help21";
		}else if(secId==22){
			result="manager/help22";
		}else if(secId==23){
			result="manager/help23";
		}else if(secId==24){
			result="manager/help24";
		}else if(secId==25){
			result="manager/help25";
		}else if(secId==26){
			result="manager/help26";
		}else if(secId==27){
			result="manager/help27";
		}else if(secId==28){
			result="manager/help28";
		}else if(secId==29){
			result="manager/help29";
		}else if(secId==30){
			result="manager/help30";
		}else if(secId==31){
			result="manager/help31";
		}else if(secId==32){
			result="manager/help32";
		}else if(secId==33){
			result="manager/help33";
		}else if(secId==34){
			result="manager/help34";
		}else if(secId==35){
			result="manager/help35";
		}else if(secId==36){
			result="manager/help36";
		}else if(secId==37){
			result="manager/help37";
		}else if(secId==38){
			result="manager/help38";
		}else if(secId==39){
			result="manager/help39";
		}else if(secId==41){
			result="manager/help40";
		}else{
			result="manager/help";
		}
		return result;
	}
	
	//添加帮助信息
	@RequestMapping("/add.se")
	public String addHelpInfo(HelpInfo helpInfo,HttpServletRequest request,@RequestParam(value="phot")MultipartFile mf){//文件上传要用MultipartFile对象来接收对象。
		//获取上传图片的真实路径
		String realPath = request.getServletContext().getRealPath("/images");
		//获取上传
		String uploadPath = realPath+"/"+mf.getOriginalFilename();
		String fileName= mf.getOriginalFilename();
		helpInfo.setPhoto(fileName);
		try {
			IOUtils.copy(mf.getInputStream(), new FileOutputStream(uploadPath));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("============进入添加帮助信息的方法==============");
		System.out.println("==========="+helpInfo.getContent());
		System.out.println("==========="+helpInfo.getSecId());
		System.out.println("==========="+helpInfo.getPhoto());
		boolean success = helpService.addInfo(helpInfo);
		return "manager/AddSuccess";
	}
	
	
	//删除帮助信息
	@RequestMapping("/delete.se")
	public String deleteInfo(int id){
		System.out.println("===========进入删除帮助信息的方法=================");
		System.out.println("==========删除id=============="+id);
		boolean success = helpService.deleteInfo(id);
		return "manager/DeleteSuccess";
	}
	
	//查询后修改

	@RequestMapping("/query.se")
	public String loadInfo(int id,Model model){
		System.out.println("********查询后修改的id**********"+id);
		List<HelpInfo> infoL = helpService.loadInfo(id);
		model.addAttribute("infoL", infoL);
	    for(HelpInfo in:infoL){
	    	System.out.println("集合中的对象id"+in.getId());
	    }
		System.out.println("*********查询后修改的集合*********"+infoL);
		return "manager/modify";
	}
	
	//修改帮助信息
	@RequestMapping("/update.se")
	public String updateInfo(HelpInfo helpInfo){
		System.out.println("=============进入修改帮助信息的方法====================");
		boolean success = helpService.updateInfo(helpInfo);

		return "manager/UpdateSuccess";

	}
}
